require 'MVVM.Sources.View.Adaptor'

VerticalLoopScrollRectPosAdaptor = class('VerticalLoopScrollRectPosAdaptor', Adaptor)

function VerticalLoopScrollRectPosAdaptor:ctor(gameObject, propertyName, binder)
    self.component = gameObject:GetComponent('UnityEngine.UI.LoopVerticalScrollRect')

    binder:RegisterEvent(function(viewModel, property)
        self.component.onValueChanged = function(normalizedPosition)
            if property.Value ~= normalizedPosition.y then
                property.Value = normalizedPosition.y
            end
        end
    end, function()
        self.component.onValueChanged = nil
    end, propertyName)

    self:BindProperty(propertyName, binder)
end

function VerticalLoopScrollRectPosAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if newValue == nil then
        return
    end

    self.component.verticalNormalizedPosition = newValue
end


HorizontalLoopScrollRectPosAdaptor = class('HorizontalLoopScrollRectPosAdaptor', Adaptor)

function HorizontalLoopScrollRectPosAdaptor:ctor(gameObject, propertyName, binder)
    self.component = gameObject:GetComponent('UnityEngine.UI.LoopHorizontalScrollRect')

    binder:RegisterEvent(function(viewModel, property)
        self.component.onValueChanged = function(normalizedPosition)
            if property.Value ~= normalizedPosition.x then
                property.Value = normalizedPosition.x
            end
        end
    end, function()
        self.component.onValueChanged = nil
    end, propertyName)

    self:BindProperty(propertyName, binder)
end

function HorizontalLoopScrollRectPosAdaptor:OnViewModelValueChanged(oldValue, newValue)
    if newValue == nil then
        return
    end

    self.component.horizontalNormalizedPosition = newValue
end